package org.cfd.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import java.util.List;
import org.cfd.entity.Users;
import org.cfd.service.UsersService;
import org.cfd.utils.Code;
import org.cfd.utils.Result;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器,负责处理与用户相关的HTTP请求
 *
 * @author rjs
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UsersController {

  @Resource private UsersService usersService;

  /**
   * 创建新用户(注册)
   *
   * @param user 用户注册信息
   * @return Result
   */
  @PostMapping("/register")
  public Result register(@RequestBody Users user) {

    // 设置默认头像
    user.setAvatar("uploads/avatar/default.jpg");

    // 保存用户信息
    boolean isSaved = usersService.save(user);

    // 判断保存是否成功
    if (!isSaved) {
      return new Result(Code.REGISTER_ERROR, "用户注册失败");
    }

    return new Result(Code.REGISTER_SUCCESS, "用户注册成功", user);
  }

  /**
   * 用户登录
   *
   * @param user 登录信息
   * @return Result
   */
  @PostMapping("/login")
  public Result login(@RequestBody Users user) {

    // 参数校验
    if (user.getUsername() == null
        || user.getUsername().isEmpty()
        || user.getPassword() == null
        || user.getPassword().isEmpty()) {
      return new Result(Code.LOGIN_ERROR, "用户名或密码不能为空");
    }

    // 查询用户信息
    Users loginUser =
        usersService.getOne(
            new QueryWrapper<Users>()
                .lambda()
                .eq(Users::getUsername, user.getUsername())
                .eq(Users::getPassword, user.getPassword()));

    // 登录失败
    if (loginUser == null) {
      return new Result(Code.LOGIN_ERROR_USER, "用户名或密码错误");
    }

    // 登录成功
    return new Result(Code.LOGIN_SUCCESS, "登录成功", loginUser);
  }

  /**
   * 获取所有用户列表
   *
   * @return Result
   */
  @GetMapping
  public Result getAllUsers() {
    List<Users> users = usersService.list();
    return new Result(Code.SELECT_SUCCESS, "查询成功", users);
  }

  /**
   * 根据ID获取用户信息
   *
   * @param id 用户ID
   * @return Result
   */
  @GetMapping("/{id}")
  public Result getUserById(@PathVariable Integer id) {
    Users user = usersService.getById(id);
    if (user == null) {
      return new Result(Code.SELECT_ERROR_ID, "未找到用户");
    }
    return new Result(Code.SELECT_SUCCESS, "查询成功", user);
  }

  /**
   * 更新用户信息
   *
   * @param id 用户ID
   * @param userUpdate 更新信息
   * @return Result
   */
  @PutMapping("/{id}")
  public Result updateUser(@PathVariable Integer id, @RequestBody Users userUpdate) {
    Users user = usersService.getById(id);
    if (user == null) {
      return new Result(Code.SELECT_ERROR_ID, "未找到用户");
    }
    userUpdate.setId(id);
    boolean isUpdated = usersService.updateById(userUpdate);
    if (!isUpdated) {
      return new Result(Code.UPDATE_ERROR, "用户更新失败");
    }
    return new Result(Code.UPDATE_SUCCESS, "用户更新成功", userUpdate);
  }

  /**
   * 删除用户
   *
   * @param id 用户ID
   * @return Result
   */
  @DeleteMapping("/{id}")
  public Result deleteUser(@PathVariable Integer id) {
    boolean isRemoved = usersService.removeById(id);
    if (!isRemoved) {
      return new Result(Code.DELETE_ERROR, "用户删除失败");
    }
    return new Result(Code.DELETE_SUCCESS, "用户删除成功");
  }
}
